/*
Code generates trajectories of marriage and schooling patterns under various counterfactuals. Generates: 
	* Figure 7: Counterfactual schooling profiles
	* Figure A.11: Counterfactual marriage profiles
	* Figure A.12: Impact of shocks on hazard rate of marriage by age
	*/

* load programs that simulate and collapse trajectories from model estimates
do "$path/3_validation_and_counterfactuals/programs"

set scheme lean2

* main estimates
simulate_trajectories, bs("_bs0")
collapse_trajectories

replace inschool_end=. if age>18
keep age mar_end unmarried_end inschool_end
tempfile main_model
save `main_model'


* counterfactual 1 - constant offer probability
***********************************************

foreach counter in c1_ c2_ c3_ c4_ {
	
	* titles
	if "`counter'"=="c1_" {
		local counter_name "No MM returns to education"
	}
	if "`counter'"=="c2_" {
		local counter_name "No preference for education"
	}
	if "`counter'"=="c3_" {
		local counter_name "No deteriorating MM returns"
	}
	if "`counter'"=="c4_" {
		local counter_name "No preference for delaying marriage"
	}

	simulate_trajectories, counter(`counter')
	collapse_trajectories

	keep age mar_end unmarried_end inschool_end

	rename mar_end mar_c
	rename unmarried_end unmarried_c
	rename inschool_end inschool_c

	merge 1:1 age using `main_model'

	gen delta_inschool=inschool_c-inschool_end
	gen delta_unmarried=unmarried_c-unmarried_end
	gen delta_mar=mar_c-mar_end

	twoway (line inschool_end age if  age<=18 & age>=13) (line inschool_c age if  age<=18 & age>=13) , name(`counter's, replace) xtitle("Age") ytitle("Fraction in school")  graphregion(color(white)) xlabel(13(1)18)    legend(order(1 "Baseline Model" 2 "`counter_name'")) legend(pos(6))

	twoway (line mar_end age if  age<=22 & age>=13) (line mar_c age if  age<=22 & age>=13) , name(`counter'm, replace) xtitle("Age") ytitle("Fraction married")  graphregion(color(white)) xlabel(13(1)22)   legend(order(1 "Baseline Model" 2 "`counter_name'")) legend(pos(6))

	grc1leg `counter's `counter'm, name(`counter', replace)

}

* Figure 7
graph combine c1_s c2_s c3_s c4_s, name(comb_s, replace) ycommon
graph export "$output/all_counterfacturals_school.eps", replace

* Figure A11
graph combine c1_m c2_m c3_m c4_m, name(comb_m, replace) ycommon
graph export "$output/all_counterfacturals_mar.eps", replace

* impact of shocks on hazard rate (Figure A12)
********************************************************************************
* risk of marriage conditional on just being dropped out of school vs. still in school with exactly same education
simulate_trajectories,  bs("_bs0")
keep if age==ed+6
keep if married==0

gen weight=0
replace weight=1802/4676 if likeschool==0 & goodgirl==1
replace weight=1157/4676 if likeschool==1 & goodgirl==1
replace weight=1345/4676 if likeschool==0 & goodgirl==0
replace weight=372/4676 if likeschool==1 & goodgirl==0

gen PrMarry2=PrMarry_high*Pr_HighOffer + PrMarry_low*Pr_LowOffer 
collapse PrMarry* [pw=weight], by(age ed inschool)
drop if age==19
twoway (line PrMarry2 age if inschool==1) (line PrMarry2 age if inschool==.), legend(order(1 "Still in school" 2 "Newly dropped out") position(6)) ytitle("Hazard rate of marriage in period")
graph export "$output/sch_shock_impact_hazardrate.png", width(4000) replace
